package com.htfyun.eink.pw.core;

import android.app.Instrumentation;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.htfyun.eink.pw.core.ctrl.PWCoreController;
import com.htfyun.eink.pw.core.ctrl.PWCoreHelper;
import com.htfyun.eink.pw.core.enums.EinkDataMode;
import com.htfyun.eink.pw.core.enums.EinkShowMode;
import com.htfyun.eink.pw.core.manager.EinkManagerInstance;

public class EinkHelper {

    /**
     * 设置 {@link View} 刷新模式
     *
     * @param view     {@link View}
     * @param dataMode 数据模式
     * @param showMode 显示模式
     */
    public static void setEinkUpdateModeWithView(@Nullable View view,
                                                 @NonNull EinkDataMode dataMode,
                                                 @NonNull EinkShowMode showMode) {
        if (view == null) {
            return;
        }
        PWCoreHelper.setEinkUpdateModeWithView(view, dataMode.getDataMode(), showMode.getShowMode());
    }

    /**
     * 强制 墨水屏 以 GC 模式强刷
     */
    public static void forceEinkFullUpdate() {

        if (Build.VERSION.SDK_INT >= 30) {
            EinkManagerInstance.getInstance().screenRefresh(true, 1);
        } else {
            new Thread(() -> {
                try {
                    Instrumentation inst = new Instrumentation();
                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_F10);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }

    public static void setValueAnimatorDurationScale(float durationScale) {
        PWCoreHelper.setValueAnimatorDurationScale(durationScale);
    }

}
